package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.listagentTypeBean;
import com.ymatou.iapi.godmch.service.listagentTypeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
/**
*查看代理类型列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ListAgentType{
	private static listagentTypeBean bean;
	private static listagentTypeCall call;
	public  agent_typeWapper agentTypeWapper=new agent_typeWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查看代理类型列表");
	}

	@Before
	public void caseUp() {
		bean=new listagentTypeBean();
		call=new listagentTypeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void Verify() throws InterruptedException {
		//获取代理
		Map<String,Object> map=new HashMap();
		map.put("mchId", bean.getMchId());
		List<agent_type> list=agentTypeWapper.selectby(map).stream().filter(p->p.getState()==0).collect(Collectors.toList());
		Logger.verifyEquals(list.size(), call.getAgentTypes().size(), "验证类型");
		list.forEach(at->{
			call.getAgentTypes().forEach(returnat->{
				if(at.getTypeTitle().equals(returnat.getTitle())) {
					Logger.verifyEquals(at.getAgentType(), returnat.getAgentType(), "验证getAgentType");
					Logger.verifyEquals(at.getRemark(), returnat.getRemark(), "验证getRemark");
					Logger.verifyEquals(at.getSeqNo(), returnat.getSeqNo(), "验证getSeqNo");
					Logger.verifyEquals(at.getDiscount(), returnat.getDiscount(), "验证Discount");
					
					Logger.verifyEquals(at.getRebateRate(), returnat.getRebateRate(), "验证RebateRate");
				}
			});
			
		});
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAgentType_001() {
		Logger.start(true,"查看代理类型列表成功");
		try {
			//新增渠道商
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
            int mchId=ccbean.getMchId();
            bean.setMchId(mchId);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证返回成功status值");
			Verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAgentType_002() {
		Logger.start(true,"查看代理类型列表,商家有废除的代理类型,成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
            int mchId=ccbean.getMchId();
            GodmchManageCallService.RemoveAgentType(mchId, ccbean.getRoleType());
            bean.setMchId(mchId);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证返回成功status值");
			Verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAgentType_003() {
		Logger.start(true,"不输入mchid失败");
		try {
//			bean.setMchId(ccbean.getMchId());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getString("status"), "验证返回成功status值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAgentType_004() {
		Logger.start(true,"输入不存在mchid返回空typelist");
		try {

			bean.setMchId(-1);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证返回成功status值");
			Logger.verifyIsNull(call.getAgentTypes(),"验证AgentTypes返回空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAgentType_005() {
		Logger.start(true,"查看代理类型列表成功,验证使用人数");
		try {
			//新增渠道商
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=3;
			for(int i=0;i<count;i++) {
				Integer agentid=GodmchManageCallService.CreateSellerMch(code);
			}
            bean.setMchId(ccbean.getMchId());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证返回成功status值");
			Verify();
			int UsedAgent=call.getAgentTypes().stream().filter(at->at.getAgentType().equals(ccbean.getRoleType())).findAny().get().getUsedAgent();
			Logger.verifyEquals(count, UsedAgent, "验证UsedAgent值");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAgentType_006() {
		Logger.start(true,"查看代理类型列表成功-含有自动继承,返佣比例");
		try {
			//新增渠道商
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
            int mchId=ccbean.getMchId();
            
            createagentBean createagentBean= GodmchManageCallService.InitCreateAgentTypeBean(mchId);
            createagentBean.setAgentMode(2);
            createagentBean.setRebateRate(new BigDecimal(2));
            GodmchManageCallService.CreateAgentTypeCall(createagentBean);
            
            bean.setMchId(mchId);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证返回成功status值");
			Verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}